package cn.liangcaomei.msmservice.service.impl;

import cn.liangcaomei.msmservice.service.MsmService;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    //发送短信
    @Override
    public boolean send(Map<String, Object> map, String phone) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tJjFRX3KKEEMazf4men", "vopK8ipdrSSVYIrzctFgJruu4FK7ew");
//                DefaultProfile.getProfile("default", "LTAI4FvvVEWiTJ3GJJqJnk7", "9st82dv7EvFk9mTjY01XXbM632fRbG");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置固定相关参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置发送短信相关参数
        request.putQueryParameter("PhoneNumbers", phone);
//        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        request.putQueryParameter("SignName", "阿里云短信测试"); //success
        request.putQueryParameter("TemplateCode", "SMS_295400170");
//        request.putQueryParameter("TemplateCode", "SMS_180051135");
        request.putQueryParameter("TemplateParam",
                JSONObject.toJSONString(map));

        //最终发送
        try {

            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
